<?php
namespace app\admin\controller;

use think\facade\View;

use app\admin\model\Category as CategoryModel;
use app\admin\model\TemplateDetail;
use app\admin\model\TemplateList;

class Category extends Base
{
    protected $types = ['终极栏目', '目录', '单页'];

    public function index()
    {
        $data = CategoryModel::select();

        $data = obj_tree($data);

        return View::fetch('index', ['data' => $data, 'types' => $this->types]);
    }

    public function info($id = 0)
    {
        if ($id != 0) {
            $data = CategoryModel::find($id);
        } else {
            $data = new CategoryModel();
        }
        
        $dataParent = obj_tree(CategoryModel::field('id, pid, name')->where('type', '<>', 2)->select());
        
        $templateList = TemplateList::order('sort desc')->column('id', 'title');

        $templateDetail = TemplateDetail::order('sort desc')->column('id', 'title');

        return View::fetch('save', [
            'data' => $data,
            'dataParent' => $dataParent,
            'types' => $this->types,
            'templateDetail' => $templateDetail,
            'templateList' => $templateList
        ]);
    }

    public function save()
    {
        if ($this->app->request->isPost()) {
            $param = $this->app->request->param();

            if ($param['name'] == '') {
                $this->error("栏目名不能为空");
            }

            try {
                if ($param['id'] != 0) {
                    CategoryModel::update([
                        'pid' => $param['pid'],
                        'name' => $param['name'],
                        'template_list_id' => $param['template_list_id'],
                        'template_detail_id' => $param['template_detail_id'],
                        'type' => $param['type'],
                        'sort' => $param['sort'],
                        'remark' => $param['remark']
                    ], ['id' => $param['id']]);
                } else {
                    CategoryModel::create([
                        'pid' => $param['pid'],
                        'name' => $param['name'],
                        'template_list_id' => $param['template_list_id'],
                        'template_detail_id' => $param['template_detail_id'],
                        'type' => $param['type'],
                        'sort' => $param['sort'],
                        'remark' => $param['remark']
                    ]);
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success('操作成功', 'category/index');
        }
    }

    public function delete($id = null)
    {
        if ($this->app->request->isAjax()) {
            if (CategoryModel::where('pid', 'in', $id)->value('id')) {
                return ['code'=>0, 'msg'=>'子栏目不为空, 若要删除请先清空子栏目'];
            }

            if (CategoryModel::destroy($id)) {
                return ['code' => 1,'msg'=>"删除成功"];
            } else {
                return ['code' => 0,'msg'=>"删除失败"];
            }
        }
    }
}
